// pages/pay/pay.js
const app = getApp()
var md5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    siteImg: "../../images/tb41.png",
    jiantou_img: "../../images/tb60.png",
    shoppic: "../../images/shopimg.png",
    addressInfo: [],
    attrInfo: [],
    goodsInfo: [],
    thumbUrl: getApp().globalData.thumbUrl,
    num: 0,
    amount: 0
  },
  onChange(event) {
    var num = event.detail;
    var that = this;
    var attrInfo = that.data.attrInfo;
    var stock = attrInfo.stock;
    var price = attrInfo.price;
    if (parseInt(num) <= parseInt(stock)) {
      var amount = parseFloat(price, 2) * num;
      that.setData({
        num: num,
        amount: amount.toFixed(2)
      })
    } else {
      that.setData({
        num: stock
      })
      wx.showToast({
        title: '库存不足',
        icon: "none",
        duration: 1500
      })

    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var ruleId = options.ruleId;
    var gid = options.gid;
    var num = options.num;
    that.setData({
      ruleId: ruleId,
      gid: gid,
      num: num,
      type: 1
    })
  },
  toaddress() {
    wx.navigateTo({
      url: '/pages/address/address?type=1', //type=1从确认订单页面进入2从个人中心进入
    })
  },
  onShow: function () {
    var that = this;
    var ruleId = that.data.ruleId;
    var gid = that.data.gid;
    var num = that.data.num;
    var type = that.data.type;
    var uid = wx.getStorageSync('uid');
    var aid = wx.getStorageSync('aid');
    var param = {
      uid: uid,
      id: gid,
      type: type,
      num: num,
      aid: aid,
      ruleId: ruleId
    }
    app.wxRequest('POST', "Index/confirmOrder", param, (res) => {
      var data = res.data;
      var addressInfo = data.addressInfo;
      if (addressInfo) {
        wx.setStorageSync('aid', addressInfo.id);
      }
      that.setData({
        addressInfo: addressInfo,
        attrInfo: data.attrInfo,
        goodsInfo: data.goodsInfo,
        amount: data.amount
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  onBridgeReady: function () {
    var that = this;
    var gid = that.data.gid;
    var num = that.data.num;
    var aid = that.data.aid;
    var ruleId = that.data.ruleId;
    var aid = wx.getStorageSync('aid');
    var uid = wx.getStorageSync('uid');
    var pid=wx.getStorageSync('pid')
    if (!aid) {
      that.showFrame("请选择收货地址");
      return false;
    }
    var param = {
      uid: uid,
      gid: gid,
      num: num,
      aid: aid,
      rule_id: ruleId,
      pid:pid
    }
    app.wxRequest('POST', "Index/launchGroup", param, (res) => {
      var code = res.code;
      if (code == 1) {
        var returnData=res.data;
        var appId = returnData.appid;
        var timeStamp = (Date.parse(new Date()) / 1000).toString();
        var pkg = 'prepay_id=' + returnData.prepay_id;
        var nonceStr = returnData.nonce_str;
        var key= returnData.key;
        var paySign = md5.hexMD5('appId=' + appId + '&nonceStr=' + nonceStr + '&package=' + pkg + '&signType=MD5&timeStamp=' + timeStamp + "&key="+key).toUpperCase();
        wx.requestPayment({
          timeStamp: timeStamp,
          nonceStr: nonceStr,
          package: pkg,
          signType: 'MD5',
          paySign: paySign,
          success: function () {
            wx.showToast({
              title: '支付成功',
              mask: true,
              icon: "none",
              duration: 1500
            })
            setTimeout(function(){
              wx.reLaunch({
                url: '../launch/launch',
              })
            },1500)
          },
          fail: function (aaa) {
            wx.showToast({
              title: '支付失败',
              mask: true,
              icon: "none",
              duration: 1500
            })
          }
        })
      } else {
        that.showFrame(res.msg);
      }
    }, (err) => {
      console.log(err.errMsg)
    })
    console.log(ruleId)
  },
  showFrame: function (text) {
    wx.showToast({
      title: text,
      icon: "none",
      duration: 1500
    })
  }
})